// pages/Calculator/Calculator.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        keysdata: [
            { name: 'AC', index: '001', type: 0 },
            { name: '+/-', index: '002', type: 0 },
            { name: '%', index: '003', type: 0 },
            { name: '÷', index: '004', type: 0 },
            { name: '7', index: '005', type: 1 },
            { name: '8', index: '006', type: 1 },
            { name: '9', index: '007', type: 1 },
            { name: '×', index: '008', type: 0 },
            { name: '4', index: '009', type: 1 },
            { name: '5', index: '0010', type: 1 },
            { name: '6', index: '0011', type: 1 },
            { name: '-', index: '0012', type: 0 },
            { name: '1', index: '0013', type: 1 },
            { name: '2', index: '0014', type: 1 },
            { name: '3', index: '0015', type: 1 },
            { name: '+', index: '0016', type: 0 },
            { name: '0', index: '0017', type: 1 },
            { name: '.', index: '0018', type: 1 },
            { name: '=', index: '0019', type: 0 },
        ],
        resultNum: '0',  //结果
        calNum1: '',  // 计算前位
        calNum2: '',  // 计算后位    
        calIcon: '',  // 计算符   
        calIconEQ: '',  // = 计算符   
        isCompute: false,  // 是否计算
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // 键盘点击事件
    keyClick(event) {
        
        var n = event.target.dataset.num;
        
        if (n.type == 1) {
            if (this.data.resultNum == '0') {
                this.data.resultNum = n.name;
            } else {
                // 是否等于过
                if (this.data.calIconEQ) {
                    this.clearScreen();
                    this.data.resultNum = '';
                }
                this.data.resultNum += n.name;
            }
            this.setData({ resultNum: this.data.resultNum })
        } else if(n.type == 0){
            
            
            // 判断执行哪个方法
            switch (n.name) {
                case 'AC': this.clearScreen(); break;
                case '+/-': this.isNegative(); break;
                case '%': this.remainder(); break;
                case '÷': this.division(); break;
                case '×': this.multiplication(); break;
                case '-': this.reduce(); break;
                case '+': this.add(); break;
                case '=': this.equalRes(); break;
            }         
        }        
    },
    // 相加
    add () {
        let data = this.data;
        if (data.calIconEQ) {
            this.setData({ calIconEQ: '' }) ;
            console.log(data)
            data.resultNum = '';
            let result = this.compute(data.calNum1, data.calNum2, '+') 
            console.log(result)
            this.setData({ calNum1: result  });
            this.setData({ calNum2: ''  });
            this.setData({ calIcon: '+' }) 
            return ;
        }
        if (data.resultNum) {
            data.calNum2 = data.resultNum;
        }
        // console.log(data.calNum1)
        if (!data.calNum1) {
            data.calNum1 = 0;
        }
        if (data.resultNum) {
            let result = this.compute(data.calNum1, data.calNum2, '+') 
            this.setData({ calNum1: result  });
            this.setData({ resultNum: result  });
            data.resultNum = '';
        }
        if (!data.calIcon) {
            data.resultNum = '';
        }
        this.setData({ calIcon: '+' }) 
    },
    // 清除
    clearScreen () {
        this.setData({ resultNum: '0' });
        this.setData({ calNum1: '' });
        this.setData({ calNum2: '' });
        this.setData({ calIcon: '' });
        this.setData({ calIconEQ: '' });
    },
    // 正负
    isNegative (){

    },
    // 余数
    remainder () {

    },
    // 除法
    division () {

    },
    // 乘法
    multiplication () {

    },
    // 减法
    reduce() {
        let data = this.data;
        console.log(data)
        // 判断是否有等号
        if (data.calIconEQ) {
            // this.setData({ calIconEQ: '' }) ;

            // return ;
        }
       
        this.setData({ calIcon: '-' }) 
    },
    // 结果
    equalRes() {
        let data = this.data;

        // 判断是否等于过
        if (data.calIconEQ) {
            // 如果只有=，没有加减乘除
            if (!data.calIcon) {
                this.setData({ calNum1: data.resultNum  });
            } else {
                // console.log(data.calNum1 + '****' + data.calNum2)
                let result = this.compute(data.calNum1, data.calNum2, data.calIcon);
                this.setData({ calNum1: result  });
                this.setData({ resultNum: this.compute(result, data.calNum2, data.calIcon)  });
            }
        } else {
            // 没有 = 的情况
            this.setData({ calIconEQ: '=' });
            if (this.data.calNum2) {
                console.log(data)
                this.setData({ calNum1: data.calNum1  });
                data.calNum2 = data.resultNum;
                // 如果 后位没有数值
                if (!data.calNum2) {
                    data.calNum2 = data.calNum1;
                }
                let result = this.compute(data.calNum1, data.calNum2, data.calIcon);
                console.log(result, data.calNum2)
                this.setData({ calNum2: data.calNum2  });
                this.setData({ resultNum: result  });
            } else {
                console.log('************')
                // if (!data.calNum2) {
                //     data.calNum2 = data.calNum1;
                // }
                // let result = this.compute(data.calNum1, data.calNum2, data.calIcon);
                this.setData({ calNum1: data.resultNum  });
                // this.setData({ calNum2: data.calNum2  });
                // this.setData({ resultNum: result  });
            }
        }       
    },
    compute(num1, num2, way) {
        let res = 0;
        if (!num1) {
            num1 = '0'
        }
        if (!num2) {
            num2 = '0'
        }
        switch(way) {
            case '%': this.remainder(); break;
            case '÷': this.division(); break;
            case '×': this.multiplication(); break;
            case '-': res = parseInt(num1) - parseInt(num2); break;
            case '+': res = parseInt(num1) + parseInt(num2); break;
        }
        return res;
    }
})